package com.vercon.commonmirror.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.vercon.commonmirror.view.music.MusicControlUtils;

/**
 * Created by csh on 2021/4/8
 * 音乐播放歌名监听工具
 */
public class MusicNameUtils {
    private static final String TAG = MusicNameUtils.class.getSimpleName();

    private static MusicNameUtils musicNameUtils;
    private final Context context;
    private IntentFilter mIntentFilter;
    private OnMusicListener mListener;

    public static MusicNameUtils getInstance(Context context) {
        if (musicNameUtils == null) {
            synchronized (MusicNameUtils.class) {
                if (musicNameUtils == null) {
                    musicNameUtils = new MusicNameUtils(context);
                }
            }
        }
        return musicNameUtils;
    }

    public MusicNameUtils(Context context) {
        this.context = context;

    }

    public void setOnMusicListener(OnMusicListener listener) {
        mListener = listener;
    }

    public interface OnMusicListener {
        void onMusicCallBack(String songName, String userName);
    }

    /**
     * 注册监听
     */
    public void onStart() {
        if (context != null) {
            initData();
        }
    }

    /**
     * 注销监听
     */
    public void onStop() {
        if (context != null) {
            context.unregisterReceiver(mReceiver);
        }
    }

    private void initData() {
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("com.android.music.metachanged");
        mIntentFilter.addAction("com.android.music.playstatechanged");
        mIntentFilter.addAction("com.android.music.playbackcomplete");
        mIntentFilter.addAction("com.android.music.queuechanged");

        mIntentFilter.addAction("com.htc.music.metachanged");
        mIntentFilter.addAction("fm.last.android.metachanged");
        mIntentFilter.addAction("com.sec.android.app.music.metachanged");
        mIntentFilter.addAction("com.nullsoft.winamp.metachanged");
        mIntentFilter.addAction("com.amazon.mp3.metachanged");
        mIntentFilter.addAction("com.miui.player.metachanged");
        mIntentFilter.addAction("com.real.IMP.metachanged");
        mIntentFilter.addAction("com.sonyericsson.music.metachanged");
        mIntentFilter.addAction("com.rdio.android.metachanged");
        mIntentFilter.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
        mIntentFilter.addAction("com.andrew.apollo.metachanged");

        mIntentFilter.addAction("com.andrew.apollo.metachanged");

        mIntentFilter.addAction("com.ting.mp3.playinfo_changed");
        mIntentFilter.addAction("com.maxmpz.audioplayer.STATUS_CHANGED");
        mIntentFilter.addAction("com.maxmpz.audioplayer.TRACK_CHANGED");

        mIntentFilter.addAction("com.kugou.android.music.metachanged");
        mIntentFilter.addAction("com.kugou.android.music.playstatechanged");
        mIntentFilter.addAction("com.kugou.android.music.playbackcomplete");
        mIntentFilter.addAction("com.kugou.android.music.queuechanged");

        mIntentFilter.addAction("com.kuwo.android.music.metachanged");
        mIntentFilter.addAction("com.kuwo.android.music.playstatechanged");
        mIntentFilter.addAction("com.coolme.android.music.metachanged");
        mIntentFilter.addAction("com.coolme.android.music.playstatechanged");

//        mIntentFilter.addAction("com.spotify.music.metachanged");
//        mIntentFilter.addAction("com.spotify.music.playstatechanged");


        mIntentFilter.addAction("com.test.broadcast.standard");
        mIntentFilter.addAction("com.test.broadcast.local");


        String mCurPlayerPackageName = MusicControlUtils.getCurMediaPlayerComponentName(context).getPackageName();
//        LogUtils.e(TAG, "MusicName: " + mCurPlayerPackageName);
        if (!TextUtils.isEmpty(mCurPlayerPackageName)) {
            mIntentFilter.addAction(mCurPlayerPackageName + ".metachanged");
//		        commandFilter.addAction(mCurPlayerPackageName + ".queuechanged");
//		        commandFilter.addAction(mCurPlayerPackageName + ".playbackcomplete");
            mIntentFilter.addAction(mCurPlayerPackageName + ".playstatechanged");

            mIntentFilter.addAction(mCurPlayerPackageName + ".music.metachanged");
//		        commandFilter.addAction(mCurPlayerPackageName + ".music.queuechanged");
//		        commandFilter.addAction(mCurPlayerPackageName + ".music.playbackcomplete");
            mIntentFilter.addAction(mCurPlayerPackageName + ".music.playstatechanged");
        }
        context.registerReceiver(mReceiver, mIntentFilter);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String track = intent.getStringExtra("track");
            String albumName = intent.getStringExtra("album");
            String artist = intent.getStringExtra("artist");
            //=============

            long music_id = intent.getLongExtra("id", 0);
            String artistName = intent.getStringExtra("artist");
            String album = intent.getStringExtra("album");
            boolean playing = intent.getBooleanExtra("playing", false);
            long duration = intent.getLongExtra("duration", 3000);
            long position = intent.getLongExtra("position", 1000);
//            LogUtils.e(TAG, "onReceive: " + "music_id:" + music_id + "  playing:" + playing + " duration:" + duration + " position:" + position);
            //=============
            if (mListener != null) {
                mListener.onMusicCallBack(track, artist);
                LogUtils.i(TAG, "track: " + track + "  artist  " + artist +"  playing  " + playing);
            } else {
                LogUtils.e(TAG, "mListener == null track: " + track + "  artist  " + artist);
            }
        }
    };
} 